import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:card_swiper/card_swiper.dart';
import 'package:path_provider/path_provider.dart';
import 'package:self32/page/borrow_page.dart';
import 'package:self32/page/mine_page.dart';
import 'package:self32/page/register_select_page.dart';
import 'package:self32/page/return_page.dart';

import '../main.dart';
import '../widget/app_bar.dart';
import 'borrow_again_page.dart';
import 'query_page.dart';



class Index extends StatefulWidget {
  const Index({Key? key}) : super(key: key);

  @override
  State<Index> createState() => _IndexState();
}

class _IndexState extends State<Index> {


  final RxList<String> bannerList = RxList();


  @override
  initState() {
    super.initState();

    // socketHelper.add(checkReader());

    _loadBanner();
  }

  Future _loadBanner() async {
    final directory = await getApplicationDocumentsDirectory();
    final directoryPath = '${directory.path}/banner';

    final directoryList = Directory(directoryPath);
    final fileList = directoryList.list();

    await for (final fileEntity in fileList) {
      final fileName = fileEntity.uri.toString().split('/').last;
      bannerList.add('$directoryPath/$fileName');
    }
  }


  @override
  void dispose() {
    clock.cancel();
    super.dispose();
  }

  _swiper(){
    return SizedBox(
      width: 450.w,
      height: 165.h,
      child: Obx(() => Swiper(
        itemBuilder: (BuildContext context,int index){
          return Container(
            clipBehavior: Clip.hardEdge,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
            ),
            child: Image.file(File(bannerList[index]), fit: BoxFit.cover,),
          );
        },
        itemCount: bannerList.length,
        autoplay: true,
        autoplayDelay: 5000,
        pagination: const SwiperPagination(),
      )),
    );
  }
  _action2(){
    return   Row(
      children: [
        InkWell(
          onTap: (){
            Get.to(()=> const Query());
          },
          child: Image.asset("assets/images/query_bg.png",height:165.h,fit: BoxFit.fitHeight,),
        ),
        SizedBox(width: 6.h,),
        InkWell(
          onTap: (){
            Get.to(()=> const BorrowAgain());
          },
          child: Image.asset("assets/images/borrow_again_bg.png",height:165.h,fit: BoxFit.fitHeight,),
        ),
        SizedBox(width: 6.h,),
        InkWell(
          onTap: (){
            Get.to(()=> const Mine());
          },
          child: Image.asset("assets/images/mine_bg.png",height:165.h,fit: BoxFit.fitHeight,),
        ),
      ],
    );
  }

  _action3(){
    return    InkWell(
      onTap: (){
        Get.to(()=> const Borrow());
      },
      child: Image.asset("assets/images/borrow_bg.png",height:165.h,fit: BoxFit.fitHeight,),
    );
  }
  _action4(){
    return    InkWell(
      onTap: (){
        Get.to(()=> const Return());
      },
      child: Image.asset("assets/images/return_bg.png",height:165.h,fit: BoxFit.fitHeight,),
    );
  }


  _version(){
    return Container(
        margin: EdgeInsets.only(top: 4.h),
        child: Text('版本号3.2.0',style: TextStyle(color: Colors.white,fontSize: 8.sp))
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
              width: double.infinity,
              height: double.infinity,
              decoration: const BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/bg.png",
                  ),
                ),
              ),
              child: Column(
                children: [
                  appBar(isIndex: true),
                  Container(
                    margin: EdgeInsets.only(top: 10.h),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Column(
                          children: [
                            _swiper(),
                            SizedBox(height: 6.h,),
                            _action2(),
                          ],
                        ),
                        SizedBox(width: 6.h,),
                        Column(
                          children: [
                            _action3(),
                            SizedBox(height: 6.h,),
                            _action4(),
                          ],
                        ),
                      ],
                    ),
                  ),
                  _version(),
                ],
              )
          ),

        ]
      )
    );
  }
}